package yxy.game.pm2.command.fortdefeat;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FortDefeatMessage;
import yxy.game.pm2.module.fortdefeat.FortDefeatHandle;

/** 副本过关斩将 过关斩将领宝箱 */
@MessageType(proto = Command.FortDefeatBox, classtype = FortDefeatMessage.FortDefeatBoxRequest.class)
public class CMD_FortDefeatBox extends Command<FortDefeatMessage.FortDefeatBoxRequest> {

	@Override
	public void execute(MessageSource messageSource, FortDefeatMessage.FortDefeatBoxRequest request) {
		String playerCode = messageSource.getTag();
		int boxId = request.getBox();

		FortDefeatMessage.FortDefeatBoxResponse.Builder response = FortDefeatMessage.FortDefeatBoxResponse.newBuilder();

		try {
			Result result = new FortDefeatHandle().boxReward(playerCode, boxId, response);
			send(messageSource, response, result);
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		}
	}

	private void send(MessageSource messageSource, FortDefeatMessage.FortDefeatBoxResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}